package com.fucai.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fucai.controller.vo.TuijianAddReqVO;
import com.fucai.entity.Fucai3d;
import com.fucai.entity.Fucai3dTuijian;
import com.fucai.mapper.Fucai3dMapper;
import com.fucai.mapper.Fucai3dTuijianMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @description: 推荐数据业务层实现类
 * @author: zhuchi
 * @create: 2025-06-17 10:38
 * @Version 1.0
 **/
@Service
public class Fucai3dTuijianService extends ServiceImpl<Fucai3dTuijianMapper, Fucai3dTuijian> {

    @Resource
    private Fucai3dMapper fucai3dMapper;

    /**
     * 添加推荐数据
     * @param reqVO
     * @return
     */
    public String addTuijianData(@Validated TuijianAddReqVO reqVO){
        LambdaQueryWrapper<Fucai3dTuijian> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Fucai3dTuijian::getTuijianQihao, reqVO.getTuijianQihao());
        queryWrapper.eq(Fucai3dTuijian::getTuijianRen, reqVO.getTuijianRen());
        Fucai3dTuijian fucai3dTuijian = this.baseMapper.selectOne(queryWrapper);
        if (null == fucai3dTuijian){
            fucai3dTuijian = new Fucai3dTuijian();
        }

        BeanUtils.copyProperties(reqVO, fucai3dTuijian);

        Fucai3d fucai3d = fucai3dMapper.getByQihao(reqVO.getTuijianQihao());
        if (null != fucai3d){
            fucai3dTuijian.setZj(fucai3d.getZj());

            String tuijianSha1Result = fucai3d.getZj().contains(reqVO.getTuijianSha1()) ? "不正确" : "正确";
            String tuijianSha2Result = "不正确";
            // 杀二不包含中奖百位、中奖十位、中奖个位
            if (!reqVO.getTuijianSha2().contains(String.valueOf(fucai3d.getZjB()))){
                if (!reqVO.getTuijianSha2().contains(String.valueOf(fucai3d.getZjS()))){
                    if (!reqVO.getTuijianSha2().contains(String.valueOf(fucai3d.getZjG()))){
                        tuijianSha2Result = "正确";
                    }
                }
            }

            String tuijianQimaResult = "不正确";
            // 七码包含中奖百位、中奖十位、中奖个位
            if (reqVO.getTuijianQima().contains(String.valueOf(fucai3d.getZjB()))){
                if (reqVO.getTuijianQima().contains(String.valueOf(fucai3d.getZjS()))){
                    if (reqVO.getTuijianQima().contains(String.valueOf(fucai3d.getZjG()))){
                        tuijianQimaResult = "正确";
                    }
                }
            }
            fucai3dTuijian.setTuijianSha1Result(tuijianSha1Result);
            fucai3dTuijian.setTuijianSha2Result(tuijianSha2Result);
            fucai3dTuijian.setTuijianQimaResult(tuijianQimaResult);
        }

        fucai3dTuijian.setCreateTime(new Date());

        String result = "";
        if (null != fucai3dTuijian.getId()){
            this.baseMapper.updateById(fucai3dTuijian);
            result = "更新成功";
        }else {
            this.baseMapper.insert(fucai3dTuijian);
            result = "添加成功";
        }

        return result;
    }

}
